package com.xd.train.member.controller;

import com.xd.train.common.result.Result;
import com.xd.train.member.dto.MemberLoginDTO;
import com.xd.train.member.dto.MemberRegisterDTO;
import com.xd.train.member.dto.MemberSendCodeDTO;
import com.xd.train.member.service.MemberService;
import com.xd.train.member.vo.MemberLoginVo;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.ResponseBody;

@RestController
@RequestMapping("/member")
public class MemberController {

    @Autowired
    private MemberService memberService;

    /**
     * 计算总人数
     * @return
     */
    @GetMapping(value = "/count")
    @ResponseBody
    public Result<Integer> count(){
        int count = memberService.count();
        return Result.success(count);
    }

    /**
     * 会员注册
     * @param memberRegisterDTO
     * @return
     */
    @PostMapping("/register")
    public Result<Long> register(@Valid MemberRegisterDTO memberRegisterDTO){
        Long memberId = memberService.register(memberRegisterDTO);
        return Result.success(memberId);
    }

    /**
     * 发送验证码
     * @param memberSendCodeDTO
     * @return
     */
    @PostMapping("/send-code")
    public Result register(@Valid @RequestBody MemberSendCodeDTO memberSendCodeDTO){
        memberService.sendCode(memberSendCodeDTO);
        return Result.success();
    }

    /**
     * 会员登录
     * @param memberLoginDTO
     * @return
     */
    @PostMapping("/login")
    public Result<MemberLoginVo> register(@Valid @RequestBody MemberLoginDTO memberLoginDTO){
        MemberLoginVo memberloginVO = memberService.Memberlogin(memberLoginDTO);
        return Result.success(memberloginVO);
    }

}
